package com.yyk.yykywd.ui;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;

import com.orhanobut.logger.Logger;
import com.yyk.yykywd.R;
import com.yyk.yykywd.base.BaseActivity;
import com.yyk.yykywd.base.BaseRecyclerAdapter;
import com.yyk.yykywd.base.BaseRecyclerHolder;
import com.yyk.yykywd.http.ApiService;
import com.yyk.yykywd.bean.IndexIndexallInfo;
import com.yyk.yykywd.bean.IndexLinelistInfo;
import com.yyk.yykywd.util.Md5Util2;
import com.yyk.yykywd.widght.diver.CustomDecoration;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import butterknife.BindView;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * 动态播报 列表
 */
public class DongtaibobaoActivity extends BaseActivity {


    @BindView(R.id.rv_dtbb)
    RecyclerView rvDtbb;
    @BindView(R.id.ll_emptyview)
    LinearLayout llEmptyview;
    private String id;
    List<IndexLinelistInfo.ReportBean> reportList = new ArrayList<>();
    private BaseRecyclerAdapter<IndexLinelistInfo.ReportBean> adapter;

    List<IndexIndexallInfo.ReportallBean> reportAllList = new ArrayList<>();//全部的动态播报
    private BaseRecyclerAdapter<IndexIndexallInfo.ReportallBean> adapter2;

    @Override
    public int getLayoutId() {
        return R.layout.activity_dongtaibobao;
    }

    @Override
    public void initView() {
        setBackArrow();
        setTitle("医共体业务线 动态播报");
    }

    @Override
    public void initData() {
        id = getBundle().getString("id");
        initAdapter();
        if (id.equals("0")) {
            //全部
            postIndexIndexall(true);
        } else {
            postIndexLinelist(true);
        }
    }

    private void initAdapter() {
        if (id.equals("0")) {
            adapter2 = new BaseRecyclerAdapter<IndexIndexallInfo.ReportallBean>(mActivity, reportAllList, R.layout.adapter_item_businesslines_f) {
                @Override
                public void convert(BaseRecyclerHolder holder, IndexIndexallInfo.ReportallBean item, int position, boolean isScrolling) {
                    holder.setText(R.id.tv_state, item.getState());
                    holder.setText(R.id.tv_createtime, item.getCreatetime());
                }
            };
            rvDtbb.addItemDecoration(new CustomDecoration(mActivity, CustomDecoration.VERTICAL_LIST, R.drawable.divider_e5_05, 0));
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mActivity);
            rvDtbb.setLayoutManager(linearLayoutManager);
            rvDtbb.setAdapter(adapter2);
            adapter2.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(RecyclerView parent, View view, int position) {
//                Bundle bundle = new Bundle();
//                bundle.putString("id", reportList.get(position).getState() + "");
////                startAct(BusinessPlan2Activity.class, bundle);

                }
            });

        } else {
            adapter = new BaseRecyclerAdapter<IndexLinelistInfo.ReportBean>(mActivity, reportList, R.layout.adapter_item_businesslines_f) {
                @Override
                public void convert(BaseRecyclerHolder holder, IndexLinelistInfo.ReportBean item, int position, boolean isScrolling) {
                    holder.setText(R.id.tv_createtime, item.getCreatetime());
                    holder.setText(R.id.tv_state, item.getState());

                }
            };
            rvDtbb.addItemDecoration(new CustomDecoration(mActivity, CustomDecoration.VERTICAL_LIST, R.drawable.divider_e5_05, 0));
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mActivity);
            rvDtbb.setLayoutManager(linearLayoutManager);
            rvDtbb.setAdapter(adapter);
            adapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(RecyclerView parent, View view, int position) {

                }
            });
        }

        /**
         * TODO 分页加载   由于接口原因没有请求 数量参数，先作罢以后再补
         */
//        rvDtbb.addOnScrollListener(new EndLessOnScrollListener(linearLayoutManager) {
//            @Override
//            public void onLoadMore(int currentPage) {
////                if (pageIndex == currentPage) {
////                    return;
////                }
//                new Handler().postDelayed(new Runnable() {
//                    public void run() {
//                        //请求网络
//                        pageIndex++;
//                        postIndexIndexall(false);
//
//                    }
//                }, 1000);
//            }
//        });
    }

    /**
     * 接口25.动态播报列表
     */

    private void postIndexLinelist(final boolean flag) {
        Map<String, String> map = new TreeMap<>();
        map.put("id", id + "");
        map.put("page", "1");
        map.put("num", "100000");
        map.put("opentime", "2019-01-01");
        map.put("shuttime", "2019-01-01");
        map.put("sign", Md5Util2.createSign(map));
        ApiService.getInstance().api.postIndexLinelist(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new Observer<IndexLinelistInfo>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(IndexLinelistInfo info) {
                        Logger.e("工作模块首页 播报列表" + info.toString());
                        if (info.getCode() == 1) {
                            if (flag) {
                                reportList.clear();
                            }
                            if (info.getReport() != null && info.getReport().size() > 0) {
                                //动态播报
                                reportList.addAll(info.getReport());
                                llEmptyview.setVisibility(View.GONE);
                                rvDtbb.setVisibility(View.VISIBLE);
                            }else {
                                llEmptyview.setVisibility(View.VISIBLE);
                                rvDtbb.setVisibility(View.GONE);
                            }
                            adapter.notifyDataSetChanged();
                        }else if (info.getCode()==2){
                            llEmptyview.setVisibility(View.VISIBLE);
                            rvDtbb.setVisibility(View.GONE);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    /**
     * 接口34.首页全部（ggk）
     *
     * @param flag
     */
    private void postIndexIndexall(final boolean flag) {
        Map<String, String> map = new TreeMap<>();
        map.put("page", 1 + "");
        map.put("num", "100000");
        map.put("sign", Md5Util2.createSign(map));
        ApiService.getInstance().api.postIndexIndexall(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new Observer<IndexIndexallInfo>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(IndexIndexallInfo info) {
                        Logger.e("首页全部 播报列表（ggk）" + info.toString());
                        if (info.getCode() == 1) {
                            //如果为true，清楚掉之前加的数据
                            if (flag) {
                                reportAllList.clear();
                            }
                            //1.播报
                            if (info.getReportall() != null && info.getReportall().size() > 0) {
                                //动态播报
                                reportAllList.addAll(info.getReportall());
                                llEmptyview.setVisibility(View.GONE);
                                rvDtbb.setVisibility(View.VISIBLE);
                            } else {
                                llEmptyview.setVisibility(View.VISIBLE);
                                rvDtbb.setVisibility(View.GONE);

                            }
                            adapter2.notifyDataSetChanged();


                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        Logger.t("异常" + e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }


}
